using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;
using HCD.Const;
using HCD.Entities;
using HCD.Mdt.Dtos;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HCD.Mdt
{
    [Route("api/[controller]/[action]"), ApiController]
    public class MdtGroupUserAppService : AsyncCrudAppService<MdtGroupUser, MdtGroupUserDto, long, MdtGroupUserGetAllInput, MdtGroupUserAddInput, MdtGroupUserEditInput, EntityDto<long>, EntityDto<long>>, IMdtGroupUserAppService
    {
        private IRepository<MdtGroupUser, long> MdtGroupUserRep { get; set; }
        public MdtGroupUserAppService(IRepository<MdtGroupUser, long> repository)
        : base(repository)
        {
            LocalizationSourceName = EnterpriseConsts.LocalizationSourceName;
            MdtGroupUserRep = repository;
        }
        [HttpPost,ActionName("Add"), NonAction]
        public override async Task<MdtGroupUserDto> CreateAsync(MdtGroupUserAddInput input)
        {
            return await base.CreateAsync(input);
        }

        [HttpPut, ActionName("Edit"), NonAction]
        public override async Task<MdtGroupUserDto> UpdateAsync(MdtGroupUserEditInput input)
        {
            return await base.UpdateAsync(input);
        }
        /// <summary>
        /// 删除MdtGroup中的一员
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete, ActionName("Delete")]
        public override async Task DeleteAsync(EntityDto<long> input)
        {
            await base.DeleteAsync(input);
        }

        [HttpGet, ActionName("Get"), NonAction]
        public override async Task<MdtGroupUserDto> GetAsync(EntityDto<long> input)
        {
            return await base.GetAsync(input);
        }
        /// <summary>
        /// 分页获取MdtGroup组员
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet, ActionName("GetList")]
        public override async Task<PagedResultDto<MdtGroupUserDto>> GetAllAsync([FromQuery]MdtGroupUserGetAllInput input)
        {
            return await base.GetAllAsync(input);
        }
        protected override IQueryable<MdtGroupUser> CreateFilteredQuery(MdtGroupUserGetAllInput input)
        {
            return base.CreateFilteredQuery(input)
                .WhereIf(string.IsNullOrWhiteSpace(input.Query), t => t.DoctorName.Contains(input.Query));
        }

        /// <summary>
        /// 批量增加Mdt组员
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task Adds([FromBody]List<MdtGroupUserAddInput> input)
        {
            foreach (var item in input)
            {
                await MdtGroupUserRep.InsertAsync(ObjectMapper.Map<MdtGroupUser>(item));
            }
        }
    }
}


